﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace WeeLibrary.Text
{
    public class SQLText
    {

        /// <summary>
        /// 转换成Like语句
        /// </summary>
        /// <param name="fieldName">字段名</param>
        /// <param name="value">值</param>
        /// <returns></returns>
        public string ConvertLikeText(string fieldName, string value)
        {

            Regex quotes = new Regex(@"'");

            if (fieldName == null || fieldName == "") return "";

            if (value == null)
            {
                value = "";
            }
            else
            {
                value = quotes.Replace(value, "''")
                              .Replace("%", "\\%")
                              .Replace("_", "\\_")
                              .Replace("?", "\\?")
                              .Replace("[", "\\[")
                              .Replace("]", "\\]");
            }

            return String.Format("{0} LIKE '%{1}%'", fieldName, value);

        }

    }
}
